package com.xiaomaoguai.core.http;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * @Description :
 * @FileName: AbstractHttpClient.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年8月12日 上午10:07:23
 * @Version:V1.00
 */
public abstract class AbstractHttpClient implements IHttpClient {

	protected final static String CTX = "http://127.0.0.1:8180/OpengovdataWeb/";

	private static final HttpClient httpClient;

	/**
	 * 用户代理，设置用户的浏览器类型
	 */
	private final static String USER_AGENT = "Mozilla/5.0";

	static {
		RequestConfig config = RequestConfig.custom().setConnectTimeout(3000).setSocketTimeout(3000).build();
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setDefaultRequestConfig(config);
		httpClient = httpClientBuilder.build();
	}

	/**
	 * 设置请求头
	 */
	protected static Header setHttpHeader() {
		return new BasicHeader(HttpHeaders.USER_AGENT, USER_AGENT);
	}

	protected static String doGetWithMapParam(String url, Map<String, String> params) {
		String result = null;
		if (StringUtils.isBlank(url)) {
			throw new IllegalArgumentException("URL is  needed!");
		}
		try {
			if (params != null && !params.isEmpty()) {
				List<NameValuePair> pairs = new ArrayList<>(params.size());
				for (Map.Entry<String, String> entry : params.entrySet()) {
					String value = entry.getValue();
					if (value != null) {
						pairs.add(new BasicNameValuePair(entry.getKey(), value));
					}
				}
				url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, "UTF-8"));
			}
			HttpGet httpGet = new HttpGet(url);
			httpGet.setHeader(setHttpHeader());
			HttpResponse response = httpClient.execute(httpGet);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != 200) {
				httpGet.abort();
				throw new RuntimeException("HttpClient,error status code :" + statusCode);
			}
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				result = EntityUtils.toString(entity, "utf-8");
				System.out.println(result);
			}
			EntityUtils.consume(entity);
		} catch (ParseException | IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String doSimpleGet(String url) {
		String result = null;
		if (StringUtils.isBlank(url)) {
			throw new IllegalArgumentException("URL is  needed!");
		}
		try {
			HttpGet httpGet = new HttpGet(url);
			httpGet.setHeader(setHttpHeader());
			HttpResponse response = httpClient.execute(httpGet);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != 200) {
				httpGet.abort();
				throw new RuntimeException("HttpClient,error status code :" + statusCode);
			}
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				result = EntityUtils.toString(entity, "utf-8");
			}
			EntityUtils.consume(entity);
		} catch (ParseException | IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public String doGet(String url) {
		return doSimpleGet(url);

	}

	@Override
	public String doPost(String url) {
		return doSimpleGet(url);
	}
}
